package Ep06_BinaryTree._0572;

import Ep06_BinaryTree.TreeNode;

/**
 * @author Jimmy Zhan WORKSTATION
 * @date 2023/4/25 12:07
 * 说明：
 */

public class Mine {
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if (subRoot == null) return true;           // 空树是任何树的子树（因为每个叶子节点的左右孩子都是null，都可以与空树相等）
        else if (root == null) return false;        // 如果 subRoot 非空，root为空，一定false
        return Compare(root,subRoot) || isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
    }

    public boolean Compare(TreeNode left, TreeNode right){
        if (left == null && right == null) return true;
        else if (left != null && right == null) return false;
        else if (right != null && left == null) return false;
        else if (right.val != left.val) return false;
        else {
            boolean resLeft = Compare(left.left, right.left);
            boolean resRight = Compare(left.right, right.right);
            boolean result = resLeft && resRight;
            return result;
        }
    }
}
